import java.io.*;
import java.util.*;

public class 英语听写程序 {
    public static void main(String[] args) throws IOException {
        //获取单词和翻译的文件的输入流
        BufferedReader br = new BufferedReader(new FileReader("D:\\javastudy\\juny\\English\\word.txt"));
        //创建序列号，防止文件名重复

        //创建记录错误单词的记录本
        BufferedWriter bw=new BufferedWriter(new FileWriter("D:\\javastudy\\juny\\English\\error.txt"));
        String line;
        //创建一个Map集合存放单词和翻译
        HashMap<String, String> map = new HashMap<>();
        //创建一个集合记录回答错误的单词
        ArrayList<String> errorlist = new ArrayList<>();
        //获取到每一行
        while ((line = br.readLine())!= null) {
            String[] split = line.split("，");
            //单词
            String word = split[0];
            //翻译
            String translate = split[1];
            map.put(translate, word);
        }
        //定义一个变量记录回到正确的单词
        int count=0;
       //获取键的集合
        ArrayList<String> keylist = new ArrayList<>(map.keySet());
        Collections.shuffle(keylist);
        while (true) {
            if (count==30){
                break;
            }
            String s = keylist.get(0);
            //打印单词
            System.out.println(s);
            Scanner sc = new Scanner(System.in);
            System.out.print("请输入单词：");
            String nextLine = sc.nextLine();
            if (nextLine.equals(map.get(s))) {
                System.out.println("输入正确");
                //删除回答正确的单词
                keylist.remove(s);
                count++;
                System.out.println("已正确默写了" + count + "个单词");
                //进行打乱
                Collections.shuffle(keylist);
            }else {
                System.out.println("回答错误");
                if (!errorlist.contains(map.get(s))) {
                    //写如错误的单词到文件
                    bw.write(map.get(s)+"，"+s);
                    errorlist.add(map.get(s));
                    bw.newLine();
                    bw.flush();
                    System.out.println("记录成功");
                }
            }
        }
        //释放资源
        br.close();
        bw.close();
    }
}
